﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;






public class MonsterTaskProcessor : MonoBehaviour
{
    public static MonsterTaskProcessor Instance { get; private set; }

    //SortedList (默认)是根据它的key 值字典顺序排序元素的列表
    private Dictionary<Monster, SortedList> taskList = new Dictionary<Monster, SortedList>();


    private void Awake()
    {
        if(Instance == null)
        {
            Instance = this;
        }
    }


    private void Update()
    {
        List<Monster> monsters = new List<Monster>(taskList.Keys);
        for (int i = 0; i != monsters.Count; ++i)
        {
            Monster monster = monsters[i];
            IMonsterTask tasks = Peek(monster);

            if (tasks == null)
            {
                if (!monster.animController.IsCurrAnimName(MonsterAnimName.Hit) && !monster.animController.IsCurrAnimName(MonsterAnimName.Fall))
                {
                    monster.behaviorMode.Operation();
                }
            }
            else if (tasks.ExecuteTask())
            {
                Pop(monster);
            }
        }
    }


    public void Register(Monster monster)
    {
        taskList.Add(monster, new SortedList());
    }


    public void AddTaskFront(IMonsterTask task)
    {
        SortedList slist;
        if (!taskList.TryGetValue(task.monster, out slist))
        {
            slist = new SortedList();
            taskList.Add(task.monster, slist);
        }

        float maxPriority = 0;
        if (slist.Count > 0)
        {
            maxPriority = (float)slist.GetKey(slist.Count - 1) + 1;
        }

        slist.Add(maxPriority, task);
    }


    public void AddTaskBack(IMonsterTask task)
    {
        SortedList slist;
        if (!taskList.TryGetValue(task.monster, out slist))
        {
            slist = new SortedList();
            taskList.Add(task.monster, slist);
        }

        float minPriority = 0;
        if (slist.Count > 0)
        {
            minPriority = (float)slist.GetKey(0) - 1;
        }

        slist.Add(minPriority, task);
    }



    public void AddTask(IMonsterTask task, float priority)
    {
        SortedList slist;
        if (!taskList.TryGetValue(task.monster, out slist))
        {
            slist = new SortedList();
            taskList.Add(task.monster, slist);
        }

        slist.Add(priority, task);
    }


    /// <summary>
    /// 优先处理monster 命令列表中优先级最高的AI 命令
    /// </summary>
    private IMonsterTask Peek(Monster monster)
    {
        SortedList slist;
        taskList.TryGetValue(monster, out slist);
        if (slist.Count == 0)
        {
            return null;
        }
        else
        {
            return (IMonsterTask)slist.GetByIndex(slist.Count - 1);
        }
    }


    private IMonsterTask Pop(Monster monster)
    {
        SortedList slist;
        taskList.TryGetValue(monster, out slist);
        if (slist.Count == 0)
        {
            return null;
        }

        int lastIndex = slist.Count - 1;
        IMonsterTask task = (IMonsterTask)slist.GetByIndex(lastIndex);
        slist.RemoveAt(lastIndex);
        return task;
    }
}